package 尚硅谷;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @author zhangmin
 * @create 2022-03-31 16:22
 */
public class Prod_Con_BlockingQueue {
    private static BlockingQueue<String> queue=new ArrayBlockingQueue<>(5);
    private static class Prod extends Thread{
        @Override
        public void run(){
            try {
                queue.put("product");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("produce....");
        }
    }
    private static class Consumer extends Thread{
        @Override
        public void run(){
            try {
                queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("consume....");
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            Prod prod=new Prod();
            prod.start();
        }
        for (int i = 0; i < 5; i++) {
            Consumer consumer=new Consumer();
            consumer.start();
        }
        for (int i = 0; i < 3; i++) {
            Prod prod=new Prod();
            prod.start();
        }
    }
}
